﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using XS.Tunnel.Cad.SteelBracing.Infrastructure.Extensions;

namespace XS.Tunnel.Cad.SteelBracing.Infrastructure
{

    public class FriendlyException : Exception
    {
        public FriendlyException() : base()
        {

        }

        public FriendlyException(string message, int errorCode) : base(message)
        {
            ErrorMessage = message;
            ErrorCode = errorCode;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="message"></param>
        /// <param name="errorCode"></param>
        /// <param name="innerException"></param>
        public FriendlyException(string message, int errorCode, Exception innerException) : base(message, innerException)
        {
            ErrorMessage = message;
            ErrorCode = errorCode;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        public FriendlyException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }
        /// <summary>
        /// 错误码
        /// </summary>
        public int ErrorCode { get; set; } = -1;

        /// <summary>
        /// 错误消息
        /// </summary>
        public string ErrorMessage { get; set; }


        public string Logs { get; set; }

    }


    public static class XSException
    {

        public static FriendlyException Oh(string msg, int errorCode, params object[] args)
        {
            if (args.Length > 0)
            {
                msg = msg.Formatt(args);
            }

            return new FriendlyException(msg, errorCode);
        }

        public static FriendlyException Oh(string msg, string logs = "", int errorCode = -1, params object[] args)
        {
            if (args.Length > 0)
            {
                msg = msg.Formatt(args);
            }
            var f = new FriendlyException(msg, errorCode);
            f.Logs = logs;
            return f;
        }







    }
}
